// virtualdevice.h
#ifndef VIRTUALDEVICE_H
#define VIRTUALDEVICE_H

#include <QTcpServer>
#include <QTcpSocket>
#include <QObject>
#include <QMap>
#include <QMutex>
#include <QHostAddress>

class VirtualDevice : public QObject
{
    Q_OBJECT

public:
    explicit VirtualDevice(const QString &ip, quint16 port, QObject *parent = nullptr);
    ~VirtualDevice();

    bool start();
    void stop();
    bool isRunning() const;

    QString getIp() const { return m_ip; }
    quint16 getPort() const { return m_port; }

    // 设置特定命令的响应
    void setResponse(const QString &command, const QString &response);
    QString getResponse(const QString &command) const;

signals:
    // 发送日志信息给主窗口
    void logMessage(const QString &message);

private slots:
    void onNewConnection();
    void onReadyRead();
    void onClientDisconnected();

private:
    // --- 修改部分：将 log 函数声明为 const ---
    QString generateResponse(const QString &command);
    void log(const QString &msg);
    // --- 修改结束 ---

    QString m_ip;
    quint16 m_port;
    QTcpServer *m_server;
    QMap<QTcpSocket *, QString> m_clientBuffers;
    QMutex m_mutex;
    bool m_isRunning;

    // 命令到响应的映射表
    QMap<QString, QString> m_customResponses;
    mutable QMutex m_responseMutex; // 保护 m_customResponses
};

#endif // VIRTUALDEVICE_H
